package org.sky.service.serviceImpl;

import org.apache.log4j.Logger;
import org.sky.base.DaoSupport;
import org.sky.model.PayForOrder;
import org.sky.model.Room;
import org.sky.model.RoomUsed;
import org.sky.service.MealService;
import org.sky.service.RoomService;
import org.sky.service.RoomUsedService;
import org.sky.util.ChangeTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.Query;
import java.sql.Time;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-10-22
 * Time: 下午9:30
 * To change this template use FileUtil | Settings | FileUtil Templates.
 */

@Service
@Transactional
public class RoomUsedServiceImpl extends DaoSupport<RoomUsed> implements RoomUsedService {

    private MealService mealService;
    private RoomService roomService;
    private final static Logger logger = Logger.getLogger(RoomUsedServiceImpl.class);

    /**
     * 判断时间段内K房是否被使用
     *
     * @param room      房间
     * @param startTime 使用开始时间
     * @param endTime   使用结束时间
     * @return
     */
    public boolean isBooking(Room room, Date startTime, Date endTime) {
        Query query = em.createQuery("select r from RoomUsed r where r.startTime > ?1 and r.endTime < ?2 and r.room = ?3");
        query.setParameter(1, startTime).setParameter(2, endTime).setParameter(3, room);
        List<RoomUsed> ru = query.getResultList();
        return ru.size() > 0;
    }

    /**
     * 计算总房费
     *
     * @param roomId 房间编号
     * @return
     */
    public double totalMoney(int roomId) {
        RoomUsed ru = findRoomUsedByRoomId(roomId);

        if (ru == null)
            return 0.0d;
        Date endTime = ChangeTime.parseDate(ChangeTime.getCurrentDate());

        Calendar calendar = Calendar.getInstance();
        String today = ChangeTime.formatRealDate(calendar.getTime());
        calendar.add(Calendar.DATE, -1);
        String yesterday = ChangeTime.formatRealDate(calendar.getTime());

        ru.setEndTime(endTime);

        Query roomQuery = em.createQuery("select r from Room r where r.roomId = ?1");
        roomQuery.setParameter(1, roomId);
        List<Room> rooms = roomQuery.getResultList();
        if (rooms.size() <= 0)
            return 0.0d;
        Room room = rooms.get(0);

        double money = 0.0d;
        long eight = Long.valueOf("200000");
        long twelve = Long.valueOf("240000");
        long twelve1 = Long.valueOf("000000");
        long twelve2 = Long.valueOf("120000");
        long finishTime = Long.valueOf(ChangeTime.formatTime(ru.getEndTime()));
        long beginTime = Long.valueOf(ChangeTime.formatTime(ru.getStartTime()));

        if ("包房".equals(ru.getMeal().getMealName())) {
            if (finishTime > eight && finishTime < twelve && beginTime < eight) {
                double part1 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                double part2 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), Time.valueOf("20:00:00"), Time.valueOf("00:00:00"));
                money = part1 + part2 + ru.getExtraMoney();
            } else if (finishTime > twelve1 && beginTime < eight && beginTime > twelve2) {
                double part1 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                double part2 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), Time.valueOf("20:00:00"), Time.valueOf("00:00:00"));
                double part3 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), "包房", ru.getMeal().getHoliday(), Time.valueOf("00:00:00"), Time.valueOf("05:30:00"));
                money = part1 + part2 + part3 + ru.getExtraMoney();
            } else if (finishTime > twelve1 && beginTime > eight) {
                double part1 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                double part2 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), "包房", ru.getMeal().getHoliday(), Time.valueOf("00:00:00"), Time.valueOf("05:30:00"));
                money = part1 + part2 + ru.getExtraMoney();
            } else
                money = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime()) + ru.getExtraMoney();
        } else if ("按小时收费".equals(ru.getMeal().getMealName())) {
            if (finishTime > eight && finishTime < twelve && beginTime < eight) {
                long l = ChangeTime.parseDate(today + " 20:00:00").getTime() - ru.getStartTime().getTime();
                long hour = l / (60 * 60 * 1000);
                long min = ((l / (60 * 1000)) - hour * 60);
                System.out.println("时段(12:00:00~~20:00:00)：" + hour + "小时" + min + "分钟");
                double price = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                double part1 = hour * price + min * (price / 60.0);
                l = endTime.getTime() - ChangeTime.parseDate(today + " 20:00:00").getTime();
                hour = l / (60 * 60 * 1000);
                min = ((l / (60 * 1000)) - hour * 60);
                System.out.println("时段(20:00:00~~00:00:00)：" + hour + "小时" + min + "分钟");
                price = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), Time.valueOf("20:00:00"), Time.valueOf("00:00:00"));
                double part2 = hour * price + min * (price / 60.0);
                money = part1 + part2 + ru.getExtraMoney();
            } else if (finishTime > twelve1 && beginTime < eight && beginTime > twelve2) {
                double price = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                long l = ChangeTime.parseDate(yesterday + " 20:00:00").getTime() - ru.getStartTime().getTime();
                long hour = l / (60 * 60 * 1000);
                long min = ((l / (60 * 1000)) - hour * 60);
                System.out.println("时段(12:00:00~~20:00:00)：" + hour + "小时" + min + "分钟");
                double part1 = hour * price + min * (price / 60.0);
                price = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), Time.valueOf("20:00:00"), Time.valueOf("00:00:00"));
                l = ChangeTime.parseDate(today + " 00:00:00").getTime() - ChangeTime.parseDate(yesterday + " 20:00:00").getTime();
                hour = l / (60 * 60 * 1000);
                min = ((l / (60 * 1000)) - hour * 60);
                System.out.println("时段(20:00:00~~00:00:00)：" + hour + "小时" + min + "分钟");
                double part2 = hour * price + min * (price / 60.0);
                double part3 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), "包房", ru.getMeal().getHoliday(), Time.valueOf("00:00:00"), Time.valueOf("05:30:00"));
                money = part1 + part2 + part3 + ru.getExtraMoney();
            } else if (finishTime > twelve1 && beginTime > eight) {
                double price = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                long l = ChangeTime.parseDate(today + " 00:00:00").getTime() - ru.getStartTime().getTime();
                long hour = l / (60 * 60 * 1000);
                long min = ((l / (60 * 1000)) - hour * 60);
                System.out.println("时段(20:00:00~~00:00:00)：" + hour + "小时" + min + "分钟");
                double part1 = hour * price + min * (price / 60.0);
                double part2 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), "包房", ru.getMeal().getHoliday(), Time.valueOf("00:00:00"), Time.valueOf("05:30:00"));
                money = part1 + part2 + ru.getExtraMoney();
            } else {
                long l = endTime.getTime() - ru.getStartTime().getTime();
                long hour = l / (60 * 60 * 1000);
                long min = ((l / (60 * 1000)) - hour * 60);
                System.out.println("时段1：" + hour + "小时" + min + "分钟");
                double price = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                money = hour * price + min * (price / 60.0) + ru.getExtraMoney();
            }
        } else if ("按人数收费".equals(ru.getMeal().getMealName())) {
            if (finishTime > eight && finishTime < twelve && beginTime < eight) {
                double part1 = ru.getSingerNum() * mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                double part2 = ru.getSingerNum() * mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), Time.valueOf("20:00:00"), Time.valueOf("00:00:00"));
                money = part1 + part2 + ru.getExtraMoney();
            } else if (finishTime > twelve1 && beginTime < eight && beginTime > twelve2) {
                double part1 = ru.getSingerNum() * mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                double part2 = ru.getSingerNum() * mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), Time.valueOf("20:00:00"), Time.valueOf("00:00:00"));
                double part3 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), "包房", ru.getMeal().getHoliday(), Time.valueOf("00:00:00"), Time.valueOf("05:30:00"));
                money = part1 + part2 + part3 + ru.getExtraMoney();
            } else if (finishTime > twelve1 && beginTime > eight) {
                double part1 = ru.getSingerNum() * mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime());
                double part2 = mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), "包房", ru.getMeal().getHoliday(), Time.valueOf("00:00:00"), Time.valueOf("05:30:00"));
                money = part1 + part2 + ru.getExtraMoney();
            } else
                money = ru.getSingerNum() * mealService.findPriceByRoomStyleAndMeal(room.getRoomStyle(), ru.getMeal().getMealName(), ru.getMeal().getHoliday(), ru.getMeal().getBeginTime(), ru.getMeal().getEndTime()) + ru.getExtraMoney();
        } else
            return 0.0d;

        DecimalFormat df = new DecimalFormat("0.00");      //设置小数点后两位
        ru.setMoney(Double.parseDouble(df.format(money)));

        try {
            roomService.clear();
            roomService.update(ru);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
            return 0.0d;
        }

        return ru.getMoney();
    }

    /**
     * 某时间段房间使用情况
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     */
    public List<RoomUsed> findByDate(Date begin, Date end) {
        Query query = em.createQuery("select r from RoomUsed r where r.createTime > ?1 and r.createTime < ?2 ");
        query.setParameter(1, begin).setParameter(2, end);
        return query.getResultList();
    }

    /**
     * 某时间段营业额
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     */
    public List<RoomUsed> revenueInTimes(Date begin, Date end) {
        Query query = em.createQuery("select r from RoomUsed r where r.createTime > ?1 and r.createTime < ?2 and pay = ?3");
        query.setParameter(1, begin).setParameter(2, end).setParameter(3, PayForOrder.PAY);
        List<RoomUsed> ru = query.getResultList();
        return ru;
    }

    public List<RoomUsed> allRoomUsed() {
        Query query = em.createQuery("from RoomUsed r");
        return query.getResultList();
    }

    /**
     * 查找房间使用记录
     *
     * @param roomId 房间编号
     * @return
     */
    public RoomUsed findRoomUsedByRoomId(int roomId) {
        Query query = em.createQuery("from RoomUsed r where r.room.roomId = ?1 and r.pay = 0 ");
        query.setParameter(1, roomId);
        List<RoomUsed> ru = query.getResultList();
        if (ru.size() <= 0)
            return null;
        return ru.get(0);
    }

    @Resource(name = "mealServiceImpl")
    public void setMealService(MealService mealService) {
        this.mealService = mealService;
    }

    @Resource(name = "roomServiceImpl")
    public void setRoomService(RoomService roomService) {
        this.roomService = roomService;
    }

}
